package com.video.zlc.audioplayer.ui;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.video.zlc.audioplayer.R;
import com.video.zlc.audioplayer.utils.DensityUtil;
import com.video.zlc.audioplayer.utils.JsonUtil;
import com.video.zlc.audioplayer.utils.LogUtil;
import com.video.zlc.audioplayer.info.QuestInfo;
import com.video.zlc.audioplayer.business.MediaBusiness;
import com.video.zlc.audioplayer.utils.NetworkStateUtil;
import com.video.zlc.audioplayer.utils.ParamsUtil;
import com.video.zlc.audioplayer.utils.StringUtil;


/**
 * Created by zlc on 2016/6/20.
 * 收听专家页面
 */
public class ListenActivity extends BaseActivity implements View.OnClickListener{

    private Context mContext;
    private ImageView im_fanhui;
    private TextView id_tv_title;
    private LinearLayout ll_voice_listen;
    private NetworkStateUtil instance;
    private QuestInfo questInfo;
    private TextView id_tv_question;
    private TextView id_tv_expert_name;
    private TextView id_tv_expert_zy;
    private ImageView id_iv_voice;
    private AnimationDrawable animationDrawable;
    private String voicePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getLayoutId());
        initView();
        initData();
        initListener();
        mContext = this;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_listen;
    }

    //初始化控件
    protected void initView() {
        im_fanhui = findView(R.id.id_iv_fanhui);
        id_tv_title = findView(R.id.id_tv_title);
        id_tv_title.setText("健康热听");
        ll_voice_listen = findView(R.id.id_ll_voice_listen);
        id_tv_question = findView(R.id.id_tv_question);
        id_tv_expert_name = findView(R.id.id_tv_expert_name);
        id_tv_expert_zy = findView(R.id.id_tv_expert_zy);
        id_iv_voice = findView(R.id.id_iv_voice);
    }

    //控件点击事件
    protected void initListener() {

        im_fanhui.setOnClickListener(this);
        ll_voice_listen.setOnClickListener(this);
    }

    //填充数据
    protected void initData() {
        instance = NetworkStateUtil.instance();
        getQuestInfo();
    }


    private void getQuestInfo() {
        LogUtil.e("getQuestInfo",ParamsUtil.getSingleVoice());
        questInfo = JsonUtil.parseJsonToBean(ParamsUtil.getSingleVoice(), QuestInfo.class);
        if(questInfo ==null){
            LogUtil.e("收听专家 info","JSON解析失败");
        }else if("1".equals(questInfo.status) && questInfo.body!=null && questInfo.body.size()>0){
            setQuestInfo(questInfo);
        }
    }

    ///设置问题信息
    private void setQuestInfo(QuestInfo questInfo) {
        QuestInfo.BodyEntity bodyEntity = questInfo.body.get(0);
        if(bodyEntity==null)
            return;
        QuestInfo.BodyEntity.AUserEntity a_user = bodyEntity.a_user;
        if(a_user!=null){
            id_tv_expert_name.setText(a_user.trueName);
            id_tv_expert_zy.setText(a_user.title);
        }
        voicePath = bodyEntity.path;
        id_tv_question.setText(bodyEntity.question);
        if(!TextUtils.isEmpty(bodyEntity.videoTime)) {
            //语音的时间
            if(StringUtil.isNumber(bodyEntity.videoTime)){
                voiceExtend(Float.valueOf(bodyEntity.videoTime));
            }
        }
    }
    //根据语音时间扩展语音对话框长度
    private void voiceExtend(float seconds) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                DensityUtil.dp2px(this,148), DensityUtil.dp2px(this,36)
        );
        params.gravity = Gravity.CENTER_VERTICAL;
        if(seconds<=60){
            int width = DensityUtil.dp2px(this, seconds * (100f / 120f));
            int width1 = params.width;
            params.width = width + width1;
        }else{
            params.width = DensityUtil.dp2px(this,192);
        }
        ll_voice_listen.setLayoutParams(params);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.id_iv_fanhui:
                finish();
                break;
            case R.id.id_ll_voice_listen:
                playVoice();
                break;
        }
    }

    /**
     * 播放语音
     */
    private void playVoice() {
        if(TextUtils.isEmpty(voicePath) || !voicePath.contains(".mp3")){
            Toast.makeText(mContext,"语音文件不合法",Toast.LENGTH_LONG).show();
            return;
        }
        if(MediaBusiness.isPlaying()){
            MediaBusiness.pause();
            stopAnimation();
        }else if(MediaBusiness.isPause()){
            startAnimation();
            MediaBusiness.resume();
        }else{
            startAnimation();
            MediaBusiness.playSound(this,voicePath, new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    stopAnimation();
                }
            });
        }
    }


    private void startAnimation(){
        id_iv_voice.setBackgroundResource(R.drawable.animation_voice);
        animationDrawable = (AnimationDrawable) id_iv_voice.getBackground();
        if (animationDrawable != null && !animationDrawable.isRunning()) {
            animationDrawable.start();
        }
    }

    private void stopAnimation(){
        if (animationDrawable != null && animationDrawable.isRunning()) {
            animationDrawable.stop();
            id_iv_voice.setBackgroundResource(R.drawable.voice_listen);
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        if(MediaBusiness.isPlaying()){
            stopAnimation();
            MediaBusiness.pause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopAnimation();
        MediaBusiness.release();
    }

}
